package cn.yuanqiao.activiti.service.impl;

import cn.yuanqiao.activiti.domain.YqdaWorkflowArchiveSave;
import cn.yuanqiao.activiti.mapper.YqdaWorkflowArchiveSaveMapper;
import cn.yuanqiao.activiti.service.IYqdaWorkflowArchiveSaveService;
import cn.yuanqiao.activiti.util.SecurityUtil;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.service.ISysUserService;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 归档流程信息Service业务层处理
 * 
 * @author lyk
 * @date 2023-07-28
 */
@Service
public class YqdaWorkflowArchiveSaveServiceImpl implements IYqdaWorkflowArchiveSaveService 
{

    @Autowired
    private SecurityUtil securityUtil;


    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ProcessEngine processEngine;


    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private YqdaWorkflowArchiveSaveMapper yqdaWorkflowArchiveSaveMapper;

    /**
     * 查询归档流程信息
     * 
     * @param ID 归档流程信息主键
     * @return 归档流程信息
     */
    @Override
    public YqdaWorkflowArchiveSave selectYqdaWorkflowArchiveSaveByID(String ID)
    {
        return yqdaWorkflowArchiveSaveMapper.selectYqdaWorkflowArchiveSaveByID(ID);
    }

    /**
     * 查询归档流程信息列表
     * 
     * @param yqdaWorkflowArchiveSave 归档流程信息
     * @return 归档流程信息
     */
    @Override
    public List<YqdaWorkflowArchiveSave> selectYqdaWorkflowArchiveSaveList(YqdaWorkflowArchiveSave yqdaWorkflowArchiveSave)
    {
        return yqdaWorkflowArchiveSaveMapper.selectYqdaWorkflowArchiveSaveList(yqdaWorkflowArchiveSave);
    }

    /**
     * 新增归档流程信息
     * 
     * @param yqdaWorkflowArchiveSave 归档流程信息
     * @return 结果
     */
    @Override
    public org.activiti.engine.runtime.ProcessInstance insertYqdaWorkflowArchiveSave(YqdaWorkflowArchiveSave yqdaWorkflowArchiveSave, Map<String,Object> virables)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();

        securityUtil.logInAs(sysUser);
        String id = String.valueOf(SnowflakeIdGenerator.generateId());
        yqdaWorkflowArchiveSave.setID(id);
        yqdaWorkflowArchiveSave.setCreateTime(DateUtils.getNowDate());
        org.activiti.engine.runtime.ProcessInstance processInstance = processEngine.getRuntimeService()
                .createProcessInstanceBuilder()
                .processDefinitionKey("WS_Archive_Save")
                .tenantId(sysUser.getTenantCode())
                .name("("+sysUser.getNickName()+")档案归档流程")
                .businessKey(id)
                .variables(virables).start();
        yqdaWorkflowArchiveSave.setInstanceId(processInstance.getId());
        yqdaWorkflowArchiveSave.setSTATE("0");
        yqdaWorkflowArchiveSave.setTITLE("档案归档流程");
        yqdaWorkflowArchiveSave.setCreateName(sysUser.getNickName());
        yqdaWorkflowArchiveSave.setCreateBy(SecurityUtils.getUsername());
        yqdaWorkflowArchiveSave.setCreateTime(DateUtils.getNowDate());
        yqdaWorkflowArchiveSave.setCreateTime(DateUtils.getNowDate());
        yqdaWorkflowArchiveSaveMapper.insertYqdaWorkflowArchiveSave(yqdaWorkflowArchiveSave);
        return processInstance;
    }

    @Override
    public int addYqdaWorkflowArchiveSave(YqdaWorkflowArchiveSave yqdaWorkflowArchiveSave) {
        return yqdaWorkflowArchiveSaveMapper.insertYqdaWorkflowArchiveSave(yqdaWorkflowArchiveSave);
    }

    /**
     * 修改归档流程信息
     * 
     * @param yqdaWorkflowArchiveSave 归档流程信息
     * @return 结果
     */
    @Override
    public int updateYqdaWorkflowArchiveSave(YqdaWorkflowArchiveSave yqdaWorkflowArchiveSave)
    {
        yqdaWorkflowArchiveSave.setUpdateTime(DateUtils.getNowDate());
        return yqdaWorkflowArchiveSaveMapper.updateYqdaWorkflowArchiveSave(yqdaWorkflowArchiveSave);
    }

    /**
     * 批量删除归档流程信息
     * 
     * @param IDs 需要删除的归档流程信息主键
     * @return 结果
     */
    @Override
    public int deleteYqdaWorkflowArchiveSaveByIDs(String[] IDs)
    {
        return yqdaWorkflowArchiveSaveMapper.deleteYqdaWorkflowArchiveSaveByIDs(IDs);
    }

    /**
     * 删除归档流程信息信息
     * 
     * @param ID 归档流程信息主键
     * @return 结果
     */
    @Override
    public int deleteYqdaWorkflowArchiveSaveByID(String ID)
    {
        return yqdaWorkflowArchiveSaveMapper.deleteYqdaWorkflowArchiveSaveByID(ID);
    }

    @Override
    public ProcessInstance insertYqdaWorkflowYArchiveSave(YqdaWorkflowArchiveSave yqdaWorkflowArchiveSave, Map<String, Object> virables) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();

        securityUtil.logInAs(sysUser);
        String id = String.valueOf(SnowflakeIdGenerator.generateId());
        yqdaWorkflowArchiveSave.setID(id);
        yqdaWorkflowArchiveSave.setCreateTime(DateUtils.getNowDate());
        ProcessInstance processInstance = processEngine.getRuntimeService()
                .createProcessInstanceBuilder()
                .processDefinitionKey("YArchive_Save")
                .tenantId(sysUser.getTenantCode())
                .name("("+sysUser.getNickName()+")档案预归档流程")
                .businessKey(id)
                .variables(virables).start();
        yqdaWorkflowArchiveSave.setInstanceId(processInstance.getId());
        yqdaWorkflowArchiveSave.setSTATE("0");
        yqdaWorkflowArchiveSave.setTITLE("档案预归档流程");
        yqdaWorkflowArchiveSave.setCreateName(sysUser.getNickName());
        yqdaWorkflowArchiveSave.setCreateBy(SecurityUtils.getUsername());
        yqdaWorkflowArchiveSave.setCreateTime(DateUtils.getNowDate());
        yqdaWorkflowArchiveSave.setCreateTime(DateUtils.getNowDate());
        yqdaWorkflowArchiveSaveMapper.insertYqdaWorkflowArchiveSave(yqdaWorkflowArchiveSave);
        return processInstance;
    }
}
